<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.example.dao.ProductDao" %>
<%@ page import="com.example.domain.Product" %>
<%@ page import="com.example.util.DBUtil" %>
<%@ page import="java.util.List" %>
<!DOCTYPE html>
<html>
<head>
    <title>商品管理 - 后台管理</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            max-width: 1200px;
            margin: 0 auto;
            background-color: white;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }
        .page-title {
            color: #333;
            margin-bottom: 20px;
            padding-bottom: 10px;
            border-bottom: 2px solid #eee;
        }
        .btn {
            display: inline-block;
            padding: 8px 16px;
            background-color: #ffffff;
            color: white;
            text-decoration: none;
            border-radius: 3px;
            border: none;
            cursor: pointer;
            margin-bottom: 20px;
        }
        .btn:hover {
            background-color: #ffffff;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        th, td {
            padding: 12px;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }
        th {
            background-color: #f8f9fa;
            font-weight: bold;
        }
        tr:hover {
            background-color: #f5f5f5;
        }
        .action-links a {
            margin-right: 10px;
            text-decoration: none;
            color: #ffffff;
        }
        .action-links a:hover {
            text-decoration: underline;
        }
        .alert {
            padding: 12px;
            margin-bottom: 20px;
            border-radius: 3px;
        }
        .alert-success {
            background-color: #ffffff;
            color: #000000;
            border: 1px solid #000000;
        }
        .alert-error {
            background-color: #ffffff;
            color: #000000;
            border: 1px solid #000000;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1 class="page-title">商品管理</h1>
        
        <% if (request.getParameter("message") != null) { %>
            <div class="alert alert-success">
                <%= request.getParameter("message") %>
            </div>
        <% } %>
        
        <% if (request.getParameter("error") != null) { %>
            <div class="alert alert-error">
                <%= request.getParameter("error") %>
            </div>
        <% } %>
        
        <a href="product_add.jsp" class="btn">添加商品</a>
        
        <table>
            <thead>
                <tr>
                    <th>ID</th>
                    <th>商品图片</th>
                    <th>商品名称</th>
                    <th>价格</th>
                    <th>类型ID</th>
                    <th>状态</th>
                    <th>是否置顶</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                <% 
                    try {
                        ProductDao productDao = new ProductDao();
                        List<Product> list = productDao.getAllProducts();
                        if (list.isEmpty()) {
                %>
                    <tr>
                        <td colspan="7" style="text-align: center;">暂无商品数据</td>
                    </tr>
                <%
                        } else {
                            for(Product product : list){
                %>
                <tr>
                    <td><%= product.getId() %></td>
                    <td>
                        <% if (product.getLogo() != null && !product.getLogo().isEmpty()) { %>
                            <img src="../../<%= product.getLogo() %>" alt="<%= product.getProduct_name() %>" style="width: 100px; height: auto;">
                        <% } else { %>
                            <img src="../../assets/images/no-image.svg" alt="暂无图片" style="width: 100px; height: auto; opacity: 0.5;">
                        <% } %>
                    </td>
                    <td><%= product.getProduct_name() %></td>
                    <td>¥<%= product.getPrice() %></td>
                    <td><%= product.getType_id() %></td>
                    <td><%= product.getStatus() == 1 ? "正常" : "下架" %></td>
                    <td>
                        <a href="product_edit.jsp?id=<%= product.getId() %>">编辑</a>
                        <a href="product_delete.jsp?id=<%= product.getId() %>" 
                           onclick="return confirm('确定要删除商品 <%= product.getProduct_name() %> 吗？')">删除</a>
                    </td>
                </tr>
                <% 
                            }
                        }
                    } catch (Exception e) {
                %>
                <tr>
                    <td colspan="7" style="color: red;">加载商品列表失败: <%= e.getMessage() %></td>
                </tr>
                <% 
                    }
                %>
            </tbody>
        </table>
    </div>
</body>
</html>